import cv2
 
cap = cv2.VideoCapture(0)  # 打开摄像头


# fourcc = cv2.VideoWriter_fourcc(*'XVID')
# fourcc = cv2.VideoWriter_fourcc(*'mp4v')
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
# fourcc = cv2.VideoWriter_fourcc('F','L','V','1')
 
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# 获取原视频的fps
fps = cap.get(5)
print(width, height, fps)
# 请确保output.avi的路径是正确的
out = cv2.VideoWriter("output.avi", fourcc, 25.0, (width, height))


while(cap.isOpened()):
    ret, frame = cap.read()
    if ret:
        out.write(frame)  # 写入帧
        cv2.imshow("frame", frame)
 
        if cv2.waitKey(1) & 0xFF == ord('q'):
           break
    else:
        break
 
cap.release()
out.release()
cv2.destroyAllWindows()